package com.example.rjb_shoping.controller;

import com.example.rjb_shoping.config.MsgResponse;
import com.example.rjb_shoping.dao.RjbCommodityDao;
import com.example.rjb_shoping.dao.RjbCustomerDao;
import com.example.rjb_shoping.entity.RjbCustomer;
import com.example.rjb_shoping.query.CustomerQuery;
import com.example.rjb_shoping.service.RjbCustomerService;
import com.example.rjb_shoping.util.Md5Util;
import com.github.pagehelper.PageInfo;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static com.caucho.hessian.io.HessianInputFactory.log;

/**
 * (RjbCustomer)表控制层
 *
 * @author makejava
 * @since 2021-06-09 20:42:53
 */
@RestController
@RequestMapping("rjbCustomer")
public class RjbCustomerController {

    @Resource
    private RedisTemplate redisTemplate;
    /**
     * 服务对象
     */
    @Resource
    private RjbCustomerService rjbCustomerService;

    @Resource
    private RjbCustomerDao rjbCustomerDao;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public RjbCustomer selectOne(Integer id) {
        return this.rjbCustomerService.queryById(id);
    }

    @Value("${server.port}")
    Integer port;

    @GetMapping("/get")
    public String get(HttpSession session) {
        return session.getAttribute("user") + ":" + port;
    }

    @GetMapping("/set")
    public String set(HttpSession session, String name) {
        RjbCustomer rjbCustomer = rjbCustomerService.selectOneName(name);
        session.setAttribute("user", rjbCustomer);
        return String.valueOf(port);
    }

    /**
     * 普通用户登录
     *
     * @param username
     * @param password
     * @param code
     * @return
     */
    @PostMapping("login")
    public MsgResponse login(HttpSession session, String username, String password, String code) {
        Map<String, Object> stats = new HashMap<>();
        Jedis jedis = new Jedis();
        //从redis中获取验证码
        String codes = jedis.get("CHECKCODE_SERVER");
        // 判断验证码是否正确，不正确，直接返回
        if (StringUtils.isEmpty(code) || !codes.equalsIgnoreCase(code)) {
            return MsgResponse.fail().add("error", "验证码错误!");
        }
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            return MsgResponse.fail().add("error", "用户名或密码不能为空!");
        }
        try {
            password = Md5Util.encodeByMd5(password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        RjbCustomer customer = rjbCustomerService.login(username, password);
        if (customer == null) {
            return MsgResponse.fail().add("error", "用户名或密码输入错误!");
        } else if (customer.getCStauts() == -1) {
            return MsgResponse.fail().add("error", "该用户状态异常!");
        } else if (rjbCustomerDao.selectNameEmil(username).getCStauts() == 2) {
            return MsgResponse.fail().add("error", "当前用户尚未激活，请查看邮箱激活");
        } else {
            session.setAttribute("user", customer); //保存到session中
            return MsgResponse.success().add("user", customer);
        }
    }


    /**
     * 消费者注册
     *
     * @param nickName
     * @param password
     * @param phone
     * @param address
     * @param emil
     * @return
     */
    @PostMapping("registered")
    public Integer registered(String nickName, String password, String phone, String address, String emil) {
        return rjbCustomerService.registered(nickName, password, phone, address, emil);
    }

    /**
     * 用户信息修改
     *
     * @param rjbCustomer
     * @return
     */
    @PostMapping("updateOne")
    public RjbCustomer updateOne(@RequestBody RjbCustomer rjbCustomer) {
        System.out.println(rjbCustomer);
        return rjbCustomerService.update(rjbCustomer);
    }

    /**
     * 用户删除（过时）
     *
     * @param id
     * @return
     */
    @GetMapping("deleteOne")
    public boolean deleteOne(Integer id) {
        return rjbCustomerService.deleteById(id);
    }

    /**
     * 分页查询
     *
     * @param customerQuery
     * @return
     */
    @GetMapping("limitList")
    public PageInfo<RjbCustomer> limitList(CustomerQuery customerQuery) {
        return rjbCustomerService.limitList(customerQuery);
    }

    /**
     * 模糊查询
     *
     * @param content
     * @return
     */
    @GetMapping("blurryList")
    public List<RjbCustomer> blurryList(String content) {
        return rjbCustomerService.likeAll(content);
    }

    /**
     * 删除用户逻辑删除
     *
     * @param id
     * @return
     */
    @GetMapping("deleteOneId")
    public boolean deleteOneId(Integer id) {
        return rjbCustomerService.deleteByIdOne(id);
    }

    /**
     * 修改用户状态
     *
     * @param name
     */
    @GetMapping("setCustomer")
    public void setCustomerByName(String name) {
        System.out.println(name);
        rjbCustomerService.setCustomerByName(name);
    }
}
